package com.example.business_oa.controller;


import cn.hutool.core.date.DateUtil;
import com.example.business_oa.service.BusiService;
import com.example.business_oa.service.FybxService;
import com.example.business_oa.service.QiDongGangweixuqiuService;
import com.example.business_oa.service.qidongFeiYongService;
import com.example.comment_oa.pojos.Budget;
import com.example.comment_oa.pojos.Standard;
import com.example.comment_oa.pojos.SysEmployee;
import com.example.comment_oa.pojos.attendance.BusinessTripAp;
import com.example.comment_oa.pojos.attendance.HolidayAp;
import com.example.comment_oa.pojos_vo.FybxVo;
import com.example.comment_oa.response.AjaxResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/businessOa")
public class QiDongFeiYongController {

    @Resource
   qidongFeiYongService qidongFeiYongService;
    @Resource
    FybxService fybxService;
    @Resource
    BusiService busiService;

    @PostMapping("/qidongFeiYongBaoXiao")
    public AjaxResponse qidongQingJiaLiuCheng(@RequestBody FybxVo fybxVo){
        List<Standard> list1= fybxService.selectStand(fybxVo.getBudgetKm());
        //根据员工ID，跟发票开票天数获取出差天数
        List<BusinessTripAp> list=busiService.busiByIdEnd(fybxVo.getEmpId(), String.valueOf(fybxVo.getInvoiceTime()));
        Date date = new Date(String.valueOf(fybxVo.getBudgetTime()));
        String year = DateUtil.format(date, "yyyy");
        String month = DateUtil.format(date, "MM");
        List<Budget> budgetList=fybxService.selectByyscs(year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());
        if(list1.size()>0){
            System.out.println(year+""+month);
            if(fybxVo.getBudgetKm().equals("住宿费")){
                if(fybxVo.getInvoiceJshj()<=list1.get(0).getStandardBz()* list.get(0).getBtTime()&&fybxVo.getInvoiceJshj()<=budgetList.get(0).getBudgetCsmoney()){
                    //新增报销主表
                    fybxService.insertBXzb(fybxVo.getLcbh(),fybxVo.getAmountTitle(),new Date(),fybxVo.getDeptName()
                            ,fybxVo.getEmpId(),1,fybxVo.getInvoiceJshj(),fybxVo.getEmpCard(),fybxVo.getEmpName());

                    //修改发票状态
                    fybxService.updateinvoicePhone(fybxVo.getInvoicePhone());
                    //修改预算初始额
                    fybxService.updateCsys(fybxVo.getInvoiceJshj(),year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());
                    //新增报销详表
                    fybxService.insertBxxb(fybxVo.getBudgetKm(),fybxVo.getInvoiceJshj(),fybxVo.getInvoicePhone(),
                            fybxVo.getLcbh(),fybxVo.getDeptName(),fybxVo.getBudgetTime());


                    //这个是关于流程的
                    HashMap<String, Object> variables = new HashMap<>();
                    String assignee1 = "TR1004";
                    qidongFeiYongService.qidongFeiYongBaoXiao(fybxVo.getEmpCard(),variables,assignee1,fybxVo.getLcbh());
//            System.err.println(Gonghao+","+variables+","+assignee1+","+fulibianhao);
                    return AjaxResponse.success();
                }else{
                    return AjaxResponse.dosentexit();
                }

            }else{
                if(fybxVo.getInvoiceJshj()<list1.get(0).getStandardBz()&&fybxVo.getInvoiceJshj()<budgetList.get(0).getBudgetCsmoney()){
                    //新增报销主表
                    fybxService.insertBXzb(fybxVo.getLcbh(),fybxVo.getAmountTitle(),new Date(),fybxVo.getDeptName()
                            ,fybxVo.getEmpId(),1,fybxVo.getInvoiceJshj(),fybxVo.getEmpCard(),fybxVo.getEmpName());

                    //修改发票状态
                    fybxService.updateinvoicePhone(fybxVo.getInvoicePhone());
                    //修改预算初始额
                    fybxService.updateCsys(fybxVo.getInvoiceJshj(),year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());

                    //新增报销详表
                    fybxService.insertBxxb(fybxVo.getBudgetKm(),fybxVo.getInvoiceJshj(),fybxVo.getInvoicePhone(),
                            fybxVo.getLcbh(),fybxVo.getDeptName(),fybxVo.getBudgetTime());

                    //这个是关于流程的
                    HashMap<String, Object> variables = new HashMap<>();
                    String assignee1 = "TR1004";
                    qidongFeiYongService.qidongFeiYongBaoXiao(fybxVo.getEmpCard(),variables,assignee1,fybxVo.getLcbh());
//            System.err.println(Gonghao+","+variables+","+assignee1+","+fulibianhao);
                    return AjaxResponse.success();
                }else{
                    return AjaxResponse.dosentexit();
                }

            }
        }else{
            if(fybxVo.getInvoiceJshj()<budgetList.get(0).getBudgetCsmoney()){
                //新增报销主表
                fybxService.insertBXzb(fybxVo.getLcbh(),fybxVo.getAmountTitle(),new Date(),fybxVo.getDeptName()
                        ,fybxVo.getEmpId(),1,fybxVo.getInvoiceJshj(),fybxVo.getEmpCard(),fybxVo.getEmpName());

                //修改发票状态
                fybxService.updateinvoicePhone(fybxVo.getInvoicePhone());
                //修改预算初始额
                fybxService.updateCsys(fybxVo.getInvoiceJshj(),year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());
                //新增报销详表
                fybxService.insertBxxb(fybxVo.getBudgetKm(),fybxVo.getInvoiceJshj(),fybxVo.getInvoicePhone(),
                        fybxVo.getLcbh(),fybxVo.getDeptName(),fybxVo.getBudgetTime());

                //这个是关于流程的
                HashMap<String, Object> variables = new HashMap<>();
                String assignee1 = "TR1004";
                qidongFeiYongService.qidongFeiYongBaoXiao(fybxVo.getEmpCard(),variables,assignee1,fybxVo.getLcbh());
//            System.err.println(Gonghao+","+variables+","+assignee1+","+fulibianhao);
                return AjaxResponse.success();


            }else{
                return AjaxResponse.dosentexit();
            }
        }




    }


}
